<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="计划名称">
          <a-input placeholder="请输入计划名称" v-decorator="['trainName',validatorRules.trainName]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="开始时间">
          <a-date-picker style="width: 100%" format='YYYY-MM-DD' v-decorator="[ 'trainBeginDate',validatorRules.trainBeginDate]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="结束时间">
          <a-date-picker style="width: 100%" format='YYYY-MM-DD' v-decorator="[ 'trainEndDate',validatorRules.trainEndDate]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="计划类型">
          <a-select placeholder="请选择计划类型" v-decorator="[ 'trainType',validatorRules.trainType]">
            <a-select-option v-for="(item,index) in planTypeList" :key="index.toString()" :value="item.value">
              {{ item.text }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="学习方式">
          <a-select placeholder="请选择学习方式" v-decorator="[ 'trainStudyType',validatorRules.trainStudyType]">
            <a-select-option v-for="(item,index) in studyTypeList" :key="index.toString()" :value="item.value">
              {{ item.text }}
            </a-select-option>
          </a-select>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
    import {httpAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import moment from "moment"
    import {ajaxGetDictItems} from '@/api/api'

    const planTypeList = [] //接收下拉框数据的对象
    const studyTypeList = [] //接收下拉框数据的对象
    export default {
        name: "TSTrainPlanInfoModal",
        data() {
            return {
                planTypeList,
                studyTypeList,
                title: "操作",
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },

                confirmLoading: false,
                form: this.$form.createForm(this),
                validatorRules: {
                    trainType:{ rules: [{ required: true, message: '请选择计划类型!' }] },
                    trainStudyType:{ rules: [{ required: true, message: '请选择学习方式!' }] },
                    trainBeginDate:{ rules: [{ required: true, message: '请选择开始时间!' }] },
                    trainEndDate:{ rules: [{ required: true, message: '请选择结束时间!' }] },
                    trainName:{ rules: [{ required: true, message: '请输入计划名称!' }] }
                },
                url: {
                    add: "/tSTrainPlanInfo/add",
                    edit: "/tSTrainPlanInfo/edit",
                },
            }
        },
        created() {
            this.initPlanTypeList();
            this.initStudyTypeList();
        },
        methods: {
            initPlanTypeList(){
                //根据字典Code, 初始化字典数组
                ajaxGetDictItems('plan_type', null).then((res) => {
                    if (res.success) {
                        this.dictOptions = res.result;
                        const result = res.result;
                        result.forEach((r) => {
                            this.planTypeList.push({
                                value: r.value,
                                text: r.text
                            })
                        })
                    }
                })
            },
            initStudyTypeList(){
                //根据字典Code, 初始化字典数组
                ajaxGetDictItems('study_type', null).then((res) => {
                    if (res.success) {
                        this.dictOptions = res.result;
                        const result = res.result;
                        result.forEach((r) => {
                            this.studyTypeList.push({
                                value: r.value,
                                text: r.text
                            })
                        })
                    }
                })
            },
            add() {
                this.edit({});
            },
            edit(record) {
                this.form.resetFields();
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model, 'trainCode', 'viewId', 'trainName', 'companyId', 'trainType', 'trainStudyType', 'trainStatus', 'isDel'))
                    //时间格式化
                    this.form.setFieldsValue({trainBeginDate: this.model.trainBeginDate ? moment(this.model.trainBeginDate) : null})
                    this.form.setFieldsValue({trainEndDate: this.model.trainEndDate ? moment(this.model.trainEndDate) : null})
                    this.form.setFieldsValue({createDate: this.model.createDate ? moment(this.model.createDate) : null})
                    this.form.setFieldsValue({updateDate: this.model.updateDate ? moment(this.model.updateDate) : null})
                });

            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        that.confirmLoading = true;
                        let httpurl = '';
                        let method = '';
                        if (!this.model.id) {
                            httpurl += this.url.add;
                            method = 'post';
                        } else {
                            httpurl += this.url.edit;
                            method = 'put';
                        }
                        let formData = Object.assign(this.model, values);
                        //时间格式化
                        formData.trainBeginDate = formData.trainBeginDate ? formData.trainBeginDate.format('YYYY-MM-DD HH:mm:ss') : null;
                        formData.trainEndDate = formData.trainEndDate ? formData.trainEndDate.format('YYYY-MM-DD HH:mm:ss') : null;
                        formData.createDate = formData.createDate ? formData.createDate.format('YYYY-MM-DD HH:mm:ss') : null;
                        formData.updateDate = formData.updateDate ? formData.updateDate.format('YYYY-MM-DD HH:mm:ss') : null;

                        console.log(formData)
                        httpAction(httpurl, formData, method).then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit('ok');
                            } else {
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                            that.close();
                        })


                    }
                })
            },
            handleCancel() {
                this.close()
            },


        }
    }
</script>

<style lang="less" scoped>

</style>